<?php
//登录成功 ---index.php

//登录不成功---提示出错

$error=array();      //保存错误信息

if(! empty ($_POST))

{
    //有表单提交，接收用户表单提交的数据

    $username=isset ($_POST['username']) ? trim ($_POST['username']): '';
    $password=isset ($_POST['password']) ? trim ($_POST['password']): '';
    header('Content-Type:text/html; charset=utf-8');
    //设置连接数据库的参数
    $servername="localhost";
    $dbusername="root";
    $dbpassword="";
    $dbname="db_user";
    //使用构造方法创建链接并选择数据库

    $mysqli=new MySQLi ($servername,$dbusername,$dbpassword,$dbname,'3306');

    //检测链接

    if ($mysqli->connect_error){

        die("连接失败，出错信息为：".$mysqli->connect_error);
    }
    $mysqli->query("set names utf8");      //告知MySQL服务器使用utf8编码进行通信
    $sql="select * from tb_user where username='$username'";
    if($rs=$mysqli->query($sql))
    {
        $row=$rs->fetch_assoc();
        if ($row['password']==$password)
        {
            session_start();
            $_SESSION['info']=array(
                'id'=>$row['uid'],
                'username'=>$username
            );
            header('Location:index.php');
            die;
        }
    }
 else {
       $error[]="用户名不存在或密码错误！" ;
    }
}
require("login_html.php");